<?php
class SinhviensController extends AppController {

	var $name = 'Sinhviens';
	var $uses = array('Sinhvien','Tgiachidoan','Chidoansv','Khoahoc');
	function index() {
		$this->Sinhvien->recursive = 0;
		$this->set('sinhviens', $this->paginate());
        $tongiaos = $this->Sinhvien->Tongiao->find('list',array('fields'=>'Tongiao.tentongiao'));
		$phuongxas = $this->Sinhvien->Phuongxa->find('list',array('fields'=>'Phuongxa.tenphuongxa'));
		$dantocs = $this->Sinhvien->Dantoc->find('list',array('fields'=>'Dantoc.tendantoc'));
        $this->set(compact('tongiaos','phuongxas','dantocs'));
	}
    function search(){
        $this->layout='search';
        //sua ds cac truogn can tim, cai nay phai khop voi cac o tim kiem tren view
        $column = array('Sinhvien.id',
                        'Sinhvien.mssv',
                        'Sinhvien.tongiao_id',
                        'Sinhvien.phuongxa_id',
                        'Sinhvien.dantoc_id',
                        'Sinhvien.hoten',
                        'Sinhvien.noisinh',
                        'Sinhvien.diachi',
                        'Sinhvien.phai',
                        'Sinhvien.sodienthoai',
                        'Sinhvien.email',
                        'Sinhvien.noivaodoan',
                        'Sinhvien.noivaodang',
                        'Sinhvien.truongthanhdoan',
                        'Sinhvien.ngaysinh',
                        'Sinhvien.ngayvaodoan',
                        'Sinhvien.ngayvaodang'
                        );
        $value = $this->params['pass'];
        //pr($value);exit;
        //$conditions[]=array();
        $ii = 14;
        for($i=0;$i<count($column);$i++){
            if($value[$i]!="null"){
                if($i<14){
                    if($i==2||$i==3||$i==4){
                        $conditions[] = array($column[$i]=>$value[$i]);//check 3 cai selectbox
                    }else{
                        $conditions[] = array($column[$i].' Like'=>'%'.$value[$i].'%');   //check text input
                    }
                }else{
                    if($value[$ii]!="null"){
                        $conditions[] = array($column[$i].">="."'".$value[$ii++]."'",$column[$i]."<="."'".$value[$ii++]."'");//check form ngay thang
                    }
                }
            }
        }
        //$conditions[] = array($column[14].">="."'".$value[14]."'",$column[14]."<="."'".$value[14+1]."'");
        //$conditions[] = array($column[15].">="."'".$value[16]."'",$column[15]."<="."'".$value[16+1]."'");  
        //$conditions[] = array($column[16].">="."'".$value[18]."'",$column[16]."<="."'".$value[18+1]."'");           
        //pr($conditions);
        if(!empty($conditions)){
            $results=$this->Sinhvien->find('all',array('conditions'=>$conditions));    
        }else $results=$this->Sinhvien->find('all');
        
        $this->set('results', $results);
        //pr($results);exit;
        
    }

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid sinhvien', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('sinhvien', $this->Sinhvien->read(null, $id));
	}

	function add() {
		if (!empty($this->data))
		{
			$this->data['Sinhvien']['ngaysinh']=date('Y-m-d',strtotime($this->data['Sinhvien']['ngaysinh']));
			if(!empty($this->data['Sinhvien']['ngayvaodoan'])&& $this->data['Sinhvien']['ngayvaodoan']!='0000-00-00')
				$this->data['Sinhvien']['ngayvaodoan']=date('Y-m-d',strtotime($this->data['Sinhvien']['ngayvaodoan']));
			if(!empty($this->data['Sinhvien']['ngayvaodang'])&& $this->data['Sinhvien']['ngayvaodang']!='0000-00-00')
				$this->data['Sinhvien']['ngayvaodang']=date('Y-m-d',strtotime($this->data['Sinhvien']['ngayvaodang']));
			if(!empty($this->data['Sinhvien']['truongthanhdoan'])&& $this->data['Sinhvien']['truongthanhdoan']!='0000-00-00')
				$this->data['Sinhvien']['truongthanhdoan']=date('Y-m-d',strtotime($this->data['Sinhvien']['truongthanhdoan']));
				
			if ($this->Sinhvien->save($this->data))
			{
				$chidoansv_id = $this->data['Sinhvien']['chidoansv_id'];
				$record['Tgiachidoan']['chidoansv_id']=$chidoansv_id;
				$record['Tgiachidoan']['sinhvien_id']=$this->Sinhvien->id;
				$record['Tgiachidoan']['ngayvao']=date('Y-m-d');
				$record['Tgiachidoan']['ngaychuyen']=NULL;
				$this->Tgiachidoan->save($record);
				$this->Session->setFlash(__('Đã Lưu thông tin sinh viên', true));
				$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan','sv',$chidoansv_id));
			} else {
				$this->Session->setFlash(__('Lỗi khi lưu. Vui lòng thử lại', true));
			}
		}
		$khoahocs = $this->Khoahoc->find('list',array('fields'=>array('nambatdau')));		
		$tinhtps =$this->Sinhvien->Phuongxa->Quanhuyen->Tinhtp->find('list',array('fields'=>array('tentinhtp')));
		$tongiaos = $this->Sinhvien->Tongiao->find('list',array('fields'=>array('tentongiao')));
		$dantocs = $this->Sinhvien->Dantoc->find('list',array('fields'=>array('tendantoc')));
		$this->set(compact('tongiaos', 'phuongxas', 'dantocs','quanhuyens','tinhtps','chidoansvs','khoahocs','tgiachidoan'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data))
		{
			$this->Session->setFlash(__('Giá trị không tồn tại', true));
			$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan'));
		}
		if (!empty($this->data))
		{
			$this->data['Sinhvien']['ngaysinh']=date('Y-m-d',strtotime($this->data['Sinhvien']['ngaysinh']));
			if(!empty($this->data['Sinhvien']['ngayvaodoan'])&& $this->data['Sinhvien']['ngayvaodoan']!='0000-00-00')
				$this->data['Sinhvien']['ngayvaodoan']=date('Y-m-d',strtotime($this->data['Sinhvien']['ngayvaodoan']));
			if(!empty($this->data['Sinhvien']['ngayvaodang'])&& $this->data['Sinhvien']['ngayvaodang']!='0000-00-00')
				$this->data['Sinhvien']['ngayvaodang']=date('Y-m-d',strtotime($this->data['Sinhvien']['ngayvaodang']));
			if(!empty($this->data['Sinhvien']['truongthanhdoan'])&& $this->data['Sinhvien']['truongthanhdoan']!='0000-00-00')
				$this->data['Sinhvien']['truongthanhdoan']=date('Y-m-d',strtotime($this->data['Sinhvien']['truongthanhdoan']));
				
			if ($this->Sinhvien->save($this->data))
			{
				$sinhvien_id = $id;
				$chidoansv_id = $this->data['Sinhvien']['chidoansv_id'];
				$tgiacd = $this->Tgiachidoan->find('first',array('conditions'=>array('chidoansv_id'=>$chidoansv_id,'sinhvien_id'=>$sinhvien_id,'ngaychuyen'=>null)));
				if(empty($tgiacd))
				{
					if($this->Tgiachidoan->updateAll(array('ngaychuyen'=>'"'.date('Y-m-d').'"'),array('sinhvien_id'=>$id)))
					{
						$record['Tgiachidoan']['sinhvien_id']=$id;
						$record['Tgiachidoan']['chidoansv_id']=$chidoansv_id;
						$record['Tgiachidoan']['ngayvao']=date('Y-m-d');
						$record['Tgiachidoan']['ngaychuyen']=NULL;
						$this->Tgiachidoan->save($record);
					}
				}
				$this->Session->setFlash(__('Đã sửa thông tin sinh viên', true));
				$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan','sv',$chidoansv_id));
			} else {
				$this->Session->setFlash(__('Lỗi khi lưu. Vui lòng thử lại', true));
			}
		}
		if (empty($this->data))
		{			
			$this->data = $this->Sinhvien->read(null, $id);
			$this->data['Sinhvien']['ngaysinh']=date('d-m-Y',strtotime($this->data['Sinhvien']['ngaysinh']));
			if(!empty($this->data['Sinhvien']['ngayvaodoan'])&& $this->data['Sinhvien']['ngayvaodoan']!='0000-00-00')
				$this->data['Sinhvien']['ngayvaodoan']=date('d-m-Y',strtotime($this->data['Sinhvien']['ngayvaodoan']));
			if(!empty($this->data['Sinhvien']['ngayvaodang'])&& $this->data['Sinhvien']['ngayvaodang']!='0000-00-00')
				$this->data['Sinhvien']['ngayvaodang']=date('d-m-Y',strtotime($this->data['Sinhvien']['ngayvaodang']));
			if(!empty($this->data['Sinhvien']['truongthanhdoan'])&& $this->data['Sinhvien']['truongthanhdoan']!='0000-00-00')
				$this->data['Sinhvien']['truongthanhdoan']=date('d-m-Y',strtotime($this->data['Sinhvien']['truongthanhdoan']));
			$quanhuyen =$this->Sinhvien->Phuongxa->find('first',array('conditions'=>array('Phuongxa.id'=>$this->data['Sinhvien']['phuongxa_id']),'fields'=>array('quanhuyen_id')));
			$tinhtp =$this->Sinhvien->Phuongxa->Quanhuyen->find('first',array('conditions'=>array('Quanhuyen.id'=>$quanhuyen['Phuongxa']['quanhuyen_id']),'fields'=>array('tinhtp_id')));
		}
		$tgiachidoan = $this->Tgiachidoan->find('first',array('conditions'=>array('khoahoc_id !='=>NULL,'khoa_id'=>$this->Session->read('khoa_id'),'ngaychuyen'=>null,'sinhvien_id'=>$id)));
		$khoahocs = $this->Khoahoc->find('list',array('fields'=>array('nambatdau')));
		$chidoansvs = $this->Chidoansv->find('list',array('conditions'=>array('khoahoc_id'=>$tgiachidoan['Chidoansv']['khoahoc_id'],'khoa_id'=>$this->Session->read('khoa_id')),'fields'=>array('tenchidoan')));
		
		$tinhtps =$this->Sinhvien->Phuongxa->Quanhuyen->Tinhtp->find('list',array('fields'=>array('tentinhtp')));
		$quanhuyens =$this->Sinhvien->Phuongxa->Quanhuyen->find('list',array('conditions'=>array('tinhtp_id'=>$tinhtp['Quanhuyen']['tinhtp_id']),'fields'=>array('tenquanhuyen')));
		$phuongxas = $this->Sinhvien->Phuongxa->find('list',array('conditions'=>array('quanhuyen_id'=>$quanhuyen['Phuongxa']['quanhuyen_id']),'fields'=>array('tenphuongxa')));
		$tongiaos = $this->Sinhvien->Tongiao->find('list',array('fields'=>array('tentongiao')));
		$dantocs = $this->Sinhvien->Dantoc->find('list',array('fields'=>array('tendantoc')));
		$this->set(compact('tongiaos', 'phuongxas', 'dantocs','quanhuyen','tinhtp','quanhuyens','tinhtps','chidoansvs','khoahocs','tgiachidoan'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Giá trị không tồn tại', true));
			$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan'));
		}
		if ($this->Sinhvien->delete($id)) {
			$this->Session->setFlash(__('Đã Xóa Sinh viên', true));
			$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan'));
		}
		$this->Session->setFlash(__('Không thể xóa sinh viên. vui lòng thử lại', true));
		$this->redirect(array('controller'=>'chidoansvs','action' => 'caychidoan'));
	}

	function updatequanhuyen()
	{
		$this->layout='ajax';
		$tinhtp_id =$this->data['Sinhvien']['tinhtp_id']; 
		$quanhuyens =$this->Sinhvien->Phuongxa->Quanhuyen->find('list',array('conditions'=>array('tinhtp_id'=>$tinhtp_id),'fields'=>array('tenquanhuyen')));
		$this->set(compact('quanhuyens')); 
	}
	
	function updatephuongxa()
	{
		$this->layout='ajax';
		$quanhuyen_id =$this->data['Sinhvien']['quanhuyen_id']; 
		$phuongxas =$this->Sinhvien->Phuongxa->find('list',array('conditions'=>array('quanhuyen_id'=>$quanhuyen_id),'fields'=>array('tenphuongxa')));
		$this->set(compact('phuongxas'));
	}
	
	function updateempty()
	{
		$this->layout='ajax';
	}
	
	function updatechidoan()
	{
		$this->layout='ajax';
		$khoahoc_id = $this->data['Sinhvien']['khoahoc_id'];
		$chidoansvs = $this->Chidoansv->find('list',array('conditions'=>array('khoahoc_id'=>$khoahoc_id,'khoa_id'=>$this->Session->read('khoa_id')),'fields'=>array('tenchidoan')));
		$this->set(compact('chidoansvs'));
	}
}
